#!/usr/bin/perl

sub graphMul
{
  my ($img,$title, $y_units,$data_file,$station) = @_;
  
  my @stations=@{$station};
  my @graph_data_file=@{$data_file};
  my $myplot="";

  my $b=0;
  for(my $a=0;$a<(@stations-1);$a++){
     $b=$a+1;
     $myplot=$myplot."'$graph_data_file[$a]' using 1:3 t '$stations[$a]' with lines lt $b,\\
                   ";
  }
  my $c=$b+1;
  $myplot="plot ".$myplot."'$graph_data_file[$b]' using 1:3 t '$stations[$b]' with lines lt $c";

  #if(@stations==1){
  #   $myplot="plot '$graph_data_file[0]' using 1:3 t '$stations[0]' with lines lt 1";
  #}
 
  #if(@stations==2){
  #   $myplot="plot '$graph_data_file[0]' using 1:3 t '$stations[0]' with lines lt 1,\\
  #                 '$graph_data_file[1]' using 1:3 t '$stations[1]' with lines lt 2";
  #}
  #if(@stations==3){
  #   $myplot="plot '$graph_data_file[0]' using 1:3 t '$stations[0]' with lines lt 1,\\
  #                 '$graph_data_file[1]' using 1:3 t '$stations[1]' with lines lt 2,\\
  #                 '$graph_data_file[2]' using 1:3 t '$stations[2]' with lines lt 3";
  #}
  #if(@stations==5){
  #   $myplot="plot '$graph_data_file[0]' using 1:3 t '$stations[0]' with lines lt 1,\\
  #                 '$graph_data_file[1]' using 1:3 t '$stations[1]' with lines lt 2,\\
  #                 '$graph_data_file[2]' using 1:3 t '$stations[2]' with lines lt 3,\\
  #                 '$graph_data_file[3]' using 1:3 t '$stations[3]' with lines lt 4,\\
  #                 '$graph_data_file[4]' using 1:3 t '$stations[4]' with lines lt 5";
  #}
  #if(@stations==4){
  #   $myplot="plot '$graph_data_file[0]' using 1:3 t '$stations[0]' with lines lt 1,\\
  #                 '$graph_data_file[1]' using 1:3 t '$stations[1]' with lines lt 2,\\
  #                 '$graph_data_file[2]' using 1:3 t '$stations[2]' with lines lt 3,\\
  #                 '$graph_data_file[3]' using 1:3 t '$stations[3]' with lines lt 4";
  #}
  #if(@stations==6){
  #   $myplot="plot '$graph_data_file[0]' using 1:3 t '$stations[0]' with lines lt 1,\\
  #                 '$graph_data_file[1]' using 1:3 t '$stations[1]' with lines lt 2,\\
  #                 '$graph_data_file[2]' using 1:3 t '$stations[2]' with lines lt 3,\\
  #                 '$graph_data_file[3]' using 1:3 t '$stations[3]' with lines lt 4,\\
  #                 '$graph_data_file[4]' using 1:3 t '$stations[4]' with lines lt 5,\\
  #                 '$graph_data_file[5]' using 1:3 t '$stations[5]' with lines lt 6";
  #}
  #if(@stations==7){
  #   $myplot="plot '$graph_data_file[0]' using 1:3 t '$stations[0]' with lines lt 1,\\
  #                 '$graph_data_file[1]' using 1:3 t '$stations[1]' with lines lt 2,\\
  #                 '$graph_data_file[2]' using 1:3 t '$stations[2]' with lines lt 3,\\
  #                 '$graph_data_file[3]' using 1:3 t '$stations[3]' with lines lt 4,\\
  #                 '$graph_data_file[4]' using 1:3 t '$stations[4]' with lines lt 5,\\
  #                 '$graph_data_file[5]' using 1:3 t '$stations[5]' with lines lt 6,\\
  #                 '$graph_data_file[5]' using 1:3 t '$stations[5]' with lines lt 7";
  #}
 
  my $script="/var/www/html/images/script";  
  open (SCRIPT,">$script");
  print SCRIPT "
     set terminal png
     set output \"$img\"
     set size 0.9375,0.625
     set title \"$title\"
     set autoscale xy
     set data style lines
     set xlabel \"Time (MM/DD HH AM/PM GMT)\"  
     set timefmt \"%Y-%m-%d %H:%M\"
     set xdata time
     set pointsize 0.6
     set ylabel \"$y_units\" 
     set ytics nomirror
     set format x \"%m/%d\\n%l%p\"
     set grid
     set key right
     $myplot

     reset
     quit";
  close SCRIPT;
 
  
  system("gnuplot $script");
  system("rm $script");
  for(my $i=0;$i<@graph_data_file;$i++){  
    system("rm $graph_data_file[$i]");
  }    
}



sub my_graphall
{
my ($graph_data_file, $title, $y_units, $dir_path,$qc,$timezone) = @_;

open (SCRIPT,">$graph_data_file.script");

#my $xformat="%y/%m/%d\\n%l%p";
my $xformat="%y/%m/%d\\n%H:%M";
my $pointsize="set pointsize 0.4";
my $graphtype="lines";
my $myplot;
my $key;
my $current="";
my $current1="";
if(($title =~ /Direction/)||($title =~ /Direction/)){
  if($qc==0){
     $current="
       set yrange [0:360]
       set ytics (0,45,90,135,180,225,270,315,360)
       set ytics nomirror
     ";
  }
  else {
     $current="
        set yrange [0:360]
        set ytics (0,45,90,135,180,225,270,315,360)
        set ytics nomirror
        set y2range [0:360]
       #set y2tics (0,45,90,135,180,225,270,315,360)
        set y2tics nomirror
     ";
  }
}
$key="set key right";

if($qc==0){
   $myplot="'$graph_data_file' using 1:3 with $graphtype";
   $key=" set nokey";
}
else {

   $myplot="'$graph_data_file' using 1:3 axes x1y1 t '' with $graphtype, \\
     '$graph_data_file' using 1:4 axes x1y1 t 'suspect' with points 1 2";
}

 print SCRIPT "
  set terminal png
  set output \"$dir_path\"
  #640x480 default
  set size 0.9375,0.625
  set title \"$title\"
  set autoscale xy
  set autoscale y2
  set data style lines
  set xlabel \"Time (YY/MM/DD HH::MM $timezone)\" 0,-1  
  set timefmt \"%Y-%m-%d %H:%M:%S\"
  set xdata time
  set ylabel \"$y_units\" 
  $current
  set ytics nomirror
  #set format x \"$xformat\"
  $key
  set grid
  plot $myplot
  #show grid
  reset
  quit";
close SCRIPT;

system("gnuplot $graph_data_file.script");
system("rm $graph_data_file.script");
system("rm $graph_data_file");
}

sub graphStats
{
  my ($graph_data_file,$title,$units,$img,$plat) = @_;

  my @platform=@$plat;
  my $myplot="";

  
     
     $myplot="plot '$graph_data_file' using 1:3 t '$platform[0]' with lines lt 1,\\
                   '$graph_data_file' using 1:4 t '$platform[1]' with lines lt 2,\\
                   '$graph_data_file' using 1:5 t '$platform[2]' with lines lt 3,\\
                   '$graph_data_file' using 1:6 t '$platform[3]' with lines lt 4,\\
                   '$graph_data_file' using 1:7 t '$platform[4]' with lines lt 5,\\
                   '$graph_data_file' using 1:8 t '$platform[5]' with lines lt 6,\\
                   '$graph_data_file' using 1:9 t '$platform[6]' with lines lt 7";
  
     
  
  open (SCRIPT,">$graph_data_file.script");
  print SCRIPT "
     set terminal png
     set output \"$img\"
     set size 3.5,0.65
     set title \"$title\"
     set autoscale xy
     set data style lines
     set xlabel \"Time (MM/DD HH GMT)\"  
     set timefmt \"%Y-%m-%d %H\"
     set xdata time
     set pointsize 0.2
     set ylabel \"$y_units\" 
     set ytics nomirror
     set format x \"%m/%d %H\"
     set grid
     set key right

     $myplot

     reset
     quit";
  close SCRIPT;

  system("gnuplot $graph_data_file.script");
  system("rm $graph_data_file.script");
  
  system("rm $graph_data_file");
    
}


sub my_graph
{
my ($graph_data_file, $title, $y_units, $dir_path,$timeperiod,$qc,$dst,$ticstring) = @_;


open (SCRIPT,">$graph_data_file.script");

my $xformat="%m/%d\\n%l%p";
my $pointsize="set pointsize 0.4";

 if($timeperiod eq "1_day"){
   #$xformat="%l%p";
   $pointsize="set pointsize 0.8";
 }


if(!$dst){$dst=tellmeedt();}


my $graphtype="lines";
if(($timeperiod eq "1_day")||($timeperiod eq "3_day")||($timeperiod eq "1_week")){
   $graphtype="points 3 5";
}


my $myplot;
my $key;
my $current="";
my $current1="";

my $xtics="";
if($ticstring ne ""){
  $xtics="set xtics ($ticstring)";
}
if(($title =~ /Direction/)||($title =~ /Direction/)){
  if($qc==0){
$current="
set yrange [0:360]
set ytics (0,45,90,135,180,225,270,315,360)
set ytics nomirror
";
}
else {
$current="
set yrange [0:360]
set ytics (0,45,90,135,180,225,270,315,360)
set ytics nomirror
set y2range [0:360]
#set y2tics (0,45,90,135,180,225,270,315,360)
set y2tics nomirror
";
}
}
$key="set key right";

if($qc==0){
   $myplot="'$graph_data_file' using 1:3 with $graphtype";
   $key=" set nokey";
}
else {

   $myplot="'$graph_data_file' using 1:3 axes x1y1 t '' with $graphtype, \\
     '$graph_data_file' using 1:4 axes x1y1 t 'suspect' with points 1 2";
}

print SCRIPT "
set terminal png
set output \"$dir_path\"
#640x480 default
set size 0.65,0.48
#set title \"$title\"
set autoscale xy
set autoscale y2
set data style lines
set xlabel \"Time (HH AM/PM $dst)\"  
set timefmt \"%Y-%m-%d %H:%M\"
set xdata time
$pointsize
set ylabel \"$y_units\" 
$current
$xtics
set ytics nomirror
#set format x \"%b%d\\n%l%p\"
set format x \"$xformat\"
$key
set grid
#et key right
#set key top title \"$title\" box 3

  plot $myplot

#show grid
reset
quit";
close SCRIPT;

system("gnuplot $graph_data_file.script");
system("rm $graph_data_file.script");
system("rm $graph_data_file");
}



1;
